/*
 * Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
 *
 * Licensed under the Mulan PSL v2.
 * You can use this file according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of the License at
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
 * FOR MORE DETAILS.
 */

#pragma once

#include "highfive/H5File.hpp"

#include "DBManager/IRepository/Input/Property/IPBRSECTRepository.h"
#include "DBManager/Repository/HDF5/Base/MainlyRepositoryBase.h"

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
namespace PBRSECT
{
    struct IDENTITY
    {
        SG::DataStructure::Common::Id   SID;
        SG::DataStructure::Common::Id   MID;
        SG::DataStructure::Common::Id   FORM;
        SG::DataStructure::Common::Id   RECLEN;
        SG::DataStructure::Common::Real NSM;
        SG::DataStructure::Common::Id   OUTP;
        SG::DataStructure::Common::Id   OUTG;
        SG::DataStructure::Common::Id   OUTM;
        SG::DataStructure::Common::I64  SECTION_POS;
        SG::DataStructure::Common::I64  SECTION_LEN;
        SG::DataStructure::Common::Id   DOMAIN_ID;
    };

    struct SECTION
    {
        SG::DataStructure::Common::Real OHEIGHT;
        SG::DataStructure::Common::Real OWIDTH;
        SG::DataStructure::Common::I64  INP_POS;
        SG::DataStructure::Common::I64  INP_LEN;
        SG::DataStructure::Common::I64  ING_POS;
        SG::DataStructure::Common::I64  ING_LEN;
    };

    using INP = SG::DataStructure::FEM::PBRSECTData::inpOrg;
    using ING = SG::DataStructure::FEM::PBRSECTData::inpOrg;
};  // namespace PBRSECT
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::PBRSECT::IDENTITY> ()
{
    HighFive::CompoundType compoundType = {
        { "SID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "MID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "FORM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "RECLEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "NSM", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "OUTP", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "OUTG", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "OUTM", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "SECTION_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "SECTION_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "DOMAIN_ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DBManager::HDF5::PBRSECT::SECTION> ()
{
    HighFive::CompoundType compoundType = {
        { "OHEIGHT", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "OWIDTH", HighFive::create_datatype<SG::DataStructure::Common::Real> () },
        { "INP_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "INP_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "ING_POS", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "ING_LEN", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

template <>
inline HighFive::DataType HighFive::create_datatype<SG::DataStructure::FEM::PBRSECTData::inpOrg> ()
{
    HighFive::CompoundType compoundType = {
        { "EXTID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
        { "ID", HighFive::create_datatype<SG::DataStructure::Common::I64> () },
    };
    return compoundType;
}

SG_DBMANAGER_HDF5_NAMESPACE_OPEN
class PBRSECTRepository : public IPBRSECTRepository, public MainlyRepositoryBase<SG::DataStructure::FEM::PBRSECTData>
{
  public:
    using MainStruct       = SG::DataStructure::FEM::PBRSECTData;
    using SubStructSECTION = SG::DBManager::HDF5::PBRSECT::SECTION;
    using SubStructINP     = SG::DBManager::HDF5::PBRSECT::INP;
    using SubStructING     = SG::DBManager::HDF5::PBRSECT::ING;

    using NasMainStruct = SG::DBManager::HDF5::PBRSECT::IDENTITY;

    explicit PBRSECTRepository (HDF5Database& database);

    enum DATASET : std::uint8_t
    {
        MainDataset,
        IndexDataset,
        SubDataset_Section,
        SubDataset_Inp,
        SubDataset_Ing
    };

    void InitialDatasetInfo () override;

    void DumpWriteCahce () override;

    std::vector<DataType>* LoadReadCache (std::size_t loadIndex) override;

    void SortData () override;

    /**
     * @brief   : 获取所有数据
     */
    std::vector<SG::DataStructure::FEM::PBRSECTData> GetAllData () override;
    /**
     * @brief   : 将自己的数据转存为nastran的数据结构
     */
    void OwnToNastran (std::vector<SG::DataStructure::FEM::PBRSECTData>&         own,
                       std::vector<SG::DBManager::HDF5::PBRSECT::IDENTITY>& _OUT IDENTITYData,
                       std::vector<SubStructSECTION>& _OUT                       SectionData,
                       std::vector<SubStructINP>& _OUT                           inpData,
                       std::vector<SubStructING>& _OUT                           ingData,
                       bool                                                      isSort = false);

    /**
     * @brief   : 将NASTRAN的数据转存为自己的数据结构
     * @param   : IDENTITY
     * @param   : INFO
     * @param   : own
     */
    void NastranToOwn (std::vector<SG::DBManager::HDF5::PBRSECT::IDENTITY>&   IDENTITYData,
                       std::vector<SubStructSECTION>&                         sectionData,
                       std::vector<SubStructINP>&                             inpData,
                       std::vector<SubStructING>&                             ingData,
                       std::vector<SG::DataStructure::FEM::PBRSECTData>& _OUT own);
};
SG_DBMANAGER_HDF5_NAMESPACE_CLOSE
